package com.zang.tree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author ZVerify
 * @since 2022/10/30 11:43
 * @see <a href="https://leetcode.cn/problems/maximum-depth-of-binary-tree/">二叉树的最大深度</a>
 **/
public class 二叉树的最大深度 {

    public int maxDepth(TreeNode root) {

        if (root==null) return 0;
        if (root.left == null && root.right == null) return 1;
        Deque<TreeNode> list = new LinkedList<>();
        list.addLast(root);
        int sum = 0;
        while (!list.isEmpty()){

            int size = list.size();
            sum++;
            for (int i = 0 ; i<size; i++) {
                TreeNode treeNode = list.pollFirst();
                if (treeNode.left!=null) list.addLast(treeNode.left);
                if (treeNode.right!=null) list.addLast(treeNode.right);
            }
        }
        return sum;
    }
}
